<template>
  <div id="subLine-controller">
    <div>
      <div class="item">
        <input type="checkbox" :checked="showAll" @change="controlAll" />
        <span>全选</span>
      </div>
    </div>
    <div class="item-wrapper">
      <div class="item" v-for="item in subLineData">
        <div
          class="box"
          @click.stop="chooseLine(item)"
          :style="{
            borderColor: item.color,
            backgroundColor: item.checked ? item.color : 'transparent',
          }"
        ></div>
        <span :style="{ color: '#34c5cf' }">{{ item.name.slice(-3) }}</span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useLineData } from '@/store'
import {displayByName} from "@/cesiumTools/course"

const subLineData = ref([])
const showAll = ref(true)
const lineDataStore = useLineData()

// 全选，控制所有站点，站线
const controlAll = (e) => {
  const checked = e.target.checked
  const lineNames = subLineData.value.map((item) => item.name)
  const stationNames = []
  subLineData.value.forEach((item) => {
    const { stationsList } = item
    stationsList.forEach((station) => {
      stationNames.push(station.name)
    })
  })
  // console.log(lineNames, stationNames, checked,'subLine')
  // 控制地图元素显示隐藏
  displayByName(lineNames, stationNames, checked)
  // 控制ui面板显示隐藏
  subLineData.value = subLineData.value.map((item) => {
    item.checked = checked
    return item
  })
}

// 单独控制站线
const chooseLine = (item) => {
  const names = [item.name]
  const stationNames = item.stationsList.map((station) => station.name)
  // 控制ui面板显示隐藏
  item.checked = !item.checked
  // 控制地图元素显示隐藏
  displayByName(names, stationNames, item.checked)
}

onMounted(async () => {
  subLineData.value = lineDataStore.allData
})
</script>

<style scoped>
#subLine-controller {
  position: absolute;
  bottom: 0;
  left: -75%;
  transform: translateX(-50%);
  width: 73px;
  height: 240px;
  background-color: rgba(0, 0, 0, 0.3);
  border: 1px solid #664a16;
  padding: 4px;
  display: flex;
  flex-direction: column;
}

.box {
  width: 10px;
  height: 10px;
  border-width: 1px;
  border-style: solid;
  background: transparent;
  user-select: all;
  cursor: pointer;
}

.item {
  width: 65px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 3px;
  background-color: rgba(255, 255, 255, 0.2);
  border: 1px solid #885f12;
  color: #fff;
  font-size: 10px;
  pointer-events: all;
  cursor: pointer;
}

.item:hover {
  border: 1px solid #d8961a;
}

.item > span {
  line-height: 9.005px;
}

.item > input {
  outline: none;
  border: none;
  transition: all 0.3s ease;
}

.item-wrapper {
  display: flex;
  justify-content: space-between;
  align-content: space-around;
  flex-wrap: wrap;
  flex: 1;
  margin-top: 4px;
}
</style>
